<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>会议室管理</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="handleAdd">新增会议室</el-button>
      </div>
      
      <!-- 搜索区域 -->
      <el-form :inline="true" :model="queryParams" class="demo-form-inline">
        <el-form-item label="会议室名称">
          <el-input v-model="queryParams.name" placeholder="请输入会议室名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
            <el-option label="启用" :value="1"></el-option>
            <el-option label="禁用" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      
      <!-- 表格区域 -->
      <el-table
        v-loading="loading"
        :data="roomList"
        border
        style="width: 100%">
        <el-table-column
          prop="name"
          label="会议室名称"
          width="180">
        </el-table-column>
        <el-table-column
          prop="location"
          label="位置"
          width="180">
        </el-table-column>
        <el-table-column
          prop="capacity"
          label="容纳人数">
        </el-table-column>
        <el-table-column
          prop="equipment"
          label="设备信息">
        </el-table-column>
        <el-table-column
          prop="status"
          label="状态">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
              {{ scope.row.status === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="200">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.row)">编辑</el-button>
            <el-button
              size="mini"
              :type="scope.row.status === 1 ? 'danger' : 'success'"
              @click="handleToggleStatus(scope.row)">
              {{ scope.row.status === 1 ? '禁用' : '启用' }}
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页区域 -->
      <div class="pagination-container">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="queryParams.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
    
    <!-- 会议室表单对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="会议室名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入会议室名称"></el-input>
        </el-form-item>
        <el-form-item label="位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入会议室位置"></el-input>
        </el-form-item>
        <el-form-item label="容纳人数" prop="capacity">
          <el-input-number v-model="form.capacity" :min="1" :max="1000"></el-input-number>
        </el-form-item>
        <el-form-item label="设备信息" prop="equipment">
          <el-input type="textarea" v-model="form.equipment" placeholder="请输入设备信息"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMeetingRooms, getMeetingRoom, addMeetingRoom, updateMeetingRoom, deleteMeetingRoom, toggleMeetingRoomStatus } from '@/api/meeting/room'

export default {
  name: 'MeetingRoomList',
  data() {
    return {
      // 遮罩层
      loading: false,
      // 会议室列表
      roomList: [],
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        page: 1,
        size: 10,
        name: undefined,
        status: undefined
      },
      // 弹出层标题
      dialogTitle: '',
      // 是否显示弹出层
      dialogVisible: false,
      // 表单参数
      form: {
        id: undefined,
        name: '',
        location: '',
        capacity: 10,
        equipment: '',
        status: 1
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: '会议室名称不能为空', trigger: 'blur' }
        ],
        capacity: [
          { required: true, message: '容纳人数不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取会议室列表
    getList() {
      this.loading = true
      listMeetingRooms(this.queryParams).then(response => {
        this.roomList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    // 查询按钮操作
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    // 重置按钮操作
    resetQuery() {
      this.queryParams = {
        page: 1,
        size: 10,
        name: undefined,
        status: undefined
      }
      this.handleQuery()
    },
    // 新增按钮操作
    handleAdd() {
      this.reset()
      this.dialogTitle = '添加会议室'
      this.dialogVisible = true
    },
    // 编辑按钮操作
    handleEdit(row) {
      this.reset()
      getMeetingRoom(row.id).then(response => {
        this.form = response.data
        this.dialogTitle = '修改会议室'
        this.dialogVisible = true
      })
    },
    // 删除按钮操作
    handleDelete(row) {
      this.$confirm('是否确认删除会议室名称为"' + row.name + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return deleteMeetingRoom(row.id)
      }).then(() => {
        this.getList()
        this.$message.success('删除成功')
      }).catch(() => {})
    },
    // 切换状态操作
    handleToggleStatus(row) {
      this.$confirm('是否确认' + (row.status === 1 ? '禁用' : '启用') + '会议室名称为"' + row.name + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return toggleMeetingRoomStatus(row.id)
      }).then(() => {
        this.getList()
        this.$message.success((row.status === 1 ? '禁用' : '启用') + '成功')
      }).catch(() => {})
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: '',
        location: '',
        capacity: 10,
        equipment: '',
        status: 1
      }
      if (this.$refs.form) {
        this.$refs.form.resetFields()
      }
    },
    // 提交表单
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.id) {
            updateMeetingRoom(this.form).then(response => {
              this.$message.success('修改成功')
              this.dialogVisible = false
              this.getList()
            })
          } else {
            addMeetingRoom(this.form).then(response => {
              this.$message.success('新增成功')
              this.dialogVisible = false
              this.getList()
            })
          }
        }
      })
    },
    // 每页数量变化
    handleSizeChange(val) {
      this.queryParams.size = val
      this.getList()
    },
    // 当前页变化
    handleCurrentChange(val) {
      this.queryParams.page = val
      this.getList()
    }
  }
}
</script>

<style scoped>
.pagination-container {
  margin-top: 15px;
}
</style> 